package com.jwm.power.satoken;

import cn.dev33.satoken.stp.StpInterface;
import com.jwm.power.mapper.QueryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义权限加载接口实现类
 */
@Component    // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    private QueryMapper queryMapper;
    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 本 list 仅做模拟，实际项目中要根据具体业务逻辑来查询权限
        List<String> list = new ArrayList<String>();
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("tName", "JobMenus");
        objectMap.put("strCell", "authname");
        objectMap.put("strWhere", " where jobcode in (select jobcode from users where userid='"+loginId+"') and len(authname) > 0");
        List<Map<String, Object>> authList = queryMapper.queryDefinedDatas(objectMap);
        for (Map<String, Object> map : authList){
            list.add(map.get("authname").toString());
        }
        if(loginId.toString().equals("78")){
            list.add("*");
        }
        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 本 list 仅做模拟，实际项目中要根据具体业务逻辑来查询角色
        List<String> list = new ArrayList<String>();
        list.add("admin");
        list.add("super-admin");
        return list;
    }
}
